package inits

import (
	"context"
	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"srv-server/basic/global"
)

func InitAll() {
	//NaconsInit()
	MysqlInit()
	RedisInit()
}

//	func NaconsInit() {
//		data := config.NaconsConf
//		fmt.Println(data)
//		clientConfig := constant.ClientConfig{
//			NamespaceId:         data.NamespaceId, //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
//			TimeoutMs:           5000,
//			NotLoadCacheAtStart: true,
//			LogDir:              "/tmp/nacos/log",
//			CacheDir:            "/tmp/nacos/cache",
//			LogLevel:            "debug",
//		}
//		serverConfigs := []constant.ServerConfig{
//			{
//				IpAddr:      data.IpAddr,
//				ContextPath: "/nacos",
//				Port:        uint64(data.Port),
//				Scheme:      "http",
//			},
//		}
//		configClient, _ := clients.CreateConfigClient(map[string]interface{}{
//			"serverConfigs": serverConfigs,
//			"clientConfig":  clientConfig,
//		})
//		content, _ := configClient.GetConfig(vo.ConfigParam{
//			DataId: data.DataId,
//			Group:  data.Group})
//		app := config.Conf
//		json.Unmarshal([]byte(content), &app)
//		config.Conf = app
//		fmt.Println(config.Conf)
//	}
func MysqlInit() {
	var err error
	//data := config.Conf.Mysql
	//dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.Username, data.Password, data.Host, data.Port, data.Database)

	dsn := "root:ae18a618f2bec0834cd76a6bb4c4aa36@tcp(14.103.143.155:3306)/zk2?charset=utf8mb4&parseTime=True&loc=Local"
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}

}

func RedisInit() {

	global.Rdb = redis.NewClient(&redis.Options{
		Addr:     "14.103.143.155:6379",
		Password: "ae18a618f2bec0834cd76a6bb4c4aa36", // no password set
		DB:       0,                                  // use default DB
	})
	result, err := global.Rdb.Ping(context.Background()).Result()
	log.Println(result)
	if err != nil {
		panic(err)
	}
}
